package com.fujin.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fujin.gulimall.product.Vo.AttrGroupRelationVo;
import com.fujin.gulimall.product.Vo.AttrGroupWithAttrVo;
import com.fujin.gulimall.product.entity.AttrEntity;
import com.fujin.gulimall.product.service.AttrAttrgroupRelationService;
import com.fujin.gulimall.product.service.AttrService;
import com.fujin.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fujin.gulimall.product.entity.AttrGroupEntity;
import com.fujin.gulimall.product.service.AttrGroupService;
import com.fujin.common.utils.PageUtils;
import com.fujin.common.utils.R;



/**
 * 属性分组
 *
 * @author fujin
 * @email 1242257@gmail.com
 * @date 2021-12-25 10:56:06
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    ///api/product/attrgroup/225/withattr
    /**
     * 获取分类下的所有分组及属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrVo> list = attrGroupService.getAttrGroupAndWithAttr(catelogId);
        return R.ok().put("data",list);
    }

    /**
     * 新增属性分组&属性关联
     * @param attrGroupRelationVo
     * @return
     */
    //api/product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrAttrgroupRelationService.addRelationBatch(attrGroupRelationVo);
        return R.ok();
    }

    /**
     * 查询分组关联属性
     * @param attrGroupId
     * @return
     */
    //api/product/attrgroup/1/attr/relation
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> list = attrService.getRelation(attrGroupId);
        return R.ok().put("data",list);
    }

    /**
     * 查询分组未关联属性
     * @param attrGroupId
     * @param params
     * @return
     */
    //api/product/attrgroup/1/noattr/relation
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.attrNoRelation(attrGroupId,params);
        return R.ok().put("page",page);
    }

    /**
     * 删除分组关联属性
     * @param attrGroupRelationVo
     * @return
     */
    ///attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrService.relationDelete(attrGroupRelationVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//通过attrGroupId查询分类全路径 包括父路径子路径。。。
        Long[] categoryids = categoryService.findPathByattrGroupId(attrGroupId);
        attrGroup.setCatelogPath(categoryids);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
